﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{ 
    public class app_tblMediaRoom_RelatedDAL:Iapp_tblMediaRoom_Related
    {
        HKEA_CMSEntities model;
        app_tblMediaRoom_Related related;
        private app_tblMediaRoom_RelatedDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblMediaRoom_RelatedDAL GetInstance()
        {
            return new app_tblMediaRoom_RelatedDAL();
        }

        public int InsertENT(app_tblMediaRoom_Related ent)
        {
            ent.Updated_At = DateTime.Now;
            ent.Created_At = DateTime.Now;
            model.app_tblMediaRoom_Related.AddObject(ent);
            int tal= model.SaveChanges();
            ent = model.app_tblMediaRoom_Related.Where(a => a.AppMediaRoomRelatedUID > 0).OrderByDescending(a => a.MediaRoomRelatedUID).ToList().Last();
            return ent.AppMediaRoomRelatedUID;
        }

        public List<app_tblMediaRoom_Related> GetListByENT(app_tblMediaRoom_Related ent)
        {
            var ret = model.app_tblMediaRoom_Related.Where(a => a.AppMediaRoomRelatedUID > 0);
            if (ent.AppRelatedUID > 0)
                ret = ret.Where(a => a.AppRelatedUID == ent.AppRelatedUID);
            if (ent.AppMediaRoomUID > 0)
                ret = ret.Where(a => a.AppMediaRoomUID == ent.AppMediaRoomUID);
            if (ent.AppMediaRoomRelatedUID > 0)
                ret = ret.Where(a => a.AppMediaRoomRelatedUID == ent.AppMediaRoomRelatedUID);
            if (ent.RelatedUID > 0)
                ret = ret.Where(a => a.RelatedUID == ent.RelatedUID);
            if (ent.MediaRoomUID > 0)
                ret = ret.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (ent.Deleted > 0)
                ret = ret.Where(a=>a.Deleted==ent.Deleted);
            if (ent.MediaRoomRelatedUID > 0)
                ret = ret.Where(a => a.MediaRoomRelatedUID == ent.MediaRoomRelatedUID);
            if (!string.IsNullOrEmpty(ent.Category))
                ret = ret.Where(a => a.Category == ent.Category);
            return ret.ToList();
        }

        public int SaveList(List<app_tblMediaRoom_Related> toadded,List<app_tblMediaRoom_Related> todeleted,List<app_tblMediaRoom_Related> toupdated)
        {
            if (toadded != null && toadded.Count > 0)
            {
                foreach (var temp in toadded)
                {
                    var tempENT = model.app_tblMediaRoom_Related.Where(a=>a.AppRelatedUID==temp.AppRelatedUID);
                    if (temp.RelatedUID > 0)
                        tempENT = model.app_tblMediaRoom_Related.Where(a=>a.RelatedUID==temp.RelatedUID);
                    if (temp.MediaRoomUID > 0)
                        tempENT = model.app_tblMediaRoom_Related.Where(a=>a.MediaRoomUID==temp.MediaRoomUID);
                    if (temp.AppMediaRoomUID > 0)
                        tempENT = model.app_tblMediaRoom_Related.Where(a=>a.AppMediaRoomUID==temp.AppMediaRoomUID);
                    if (temp.MediaRoomUID > 0)
                        tempENT = model.app_tblMediaRoom_Related.Where(a => a.MediaRoomUID == temp.MediaRoomUID);
                    if (temp.MediaRoomRelatedUID > 0)
                        tempENT = model.app_tblMediaRoom_Related.Where(a=>a.MediaRoomRelatedUID==temp.MediaRoomRelatedUID);
                    var ent=tempENT.FirstOrDefault();
                    if (ent == null)
                    {
                        temp.Created_At = DateTime.Now;
                        temp.Updated_At = DateTime.Now;
                        model.app_tblMediaRoom_Related.AddObject(temp);
                    }
                    else
                    {
                        ent.Deleted=temp.Deleted;
                        ent.Updated_At=DateTime.Now;
                    }
                }
            }
            if (todeleted != null && todeleted.Count > 0)
            {
                foreach (var temp in todeleted)
                {
                    var tempENT = model.app_tblMediaRoom_Related.Where(a=>a.AppMediaRoomRelatedUID==temp.AppMediaRoomRelatedUID).FirstOrDefault();
                    if (tempENT != null)
                    {
                        if (tempENT.MediaRoomRelatedUID == 0 || tempENT.MediaRoomRelatedUID==null)
                        {
                            model.app_tblMediaRoom_Related.DeleteObject(tempENT);
                        }
                    }
                }
            }
            if (toupdated != null && toupdated.Count > 0)
            {
                foreach (var temp in toupdated)
                {
                    var tempENT = model.app_tblMediaRoom_Related.Where(a => a.AppMediaRoomRelatedUID == temp.AppMediaRoomRelatedUID).FirstOrDefault();
                    if (tempENT != null)
                    {
                        tempENT.AppMediaRoomUID = temp.AppMediaRoomUID;
                        tempENT.AppRelatedUID = temp.AppRelatedUID;
                        tempENT.Category = temp.Category;
                        tempENT.Deleted = temp.Deleted;
                        tempENT.IsPublish = temp.IsPublish;
                        tempENT.MediaRoomRelatedUID = temp.MediaRoomRelatedUID;
                        tempENT.MediaRoomUID = temp.MediaRoomUID;
                        tempENT.RelatedUID = temp.RelatedUID;
                        tempENT.Updated_At = DateTime.Now;
                    }
                }
            }
            return model.SaveChanges();
        }

        public List<app_tblMediaRoom_Related> GetListByENT(app_tblMediaRoom_Related ent, bool IsAddDeletedForSelect)
        {
            var ret = model.app_tblMediaRoom_Related.Where(a => a.AppMediaRoomRelatedUID > 0);
            if (ent.AppRelatedUID > 0)
                ret = ret.Where(a => a.AppRelatedUID == ent.AppRelatedUID);
            if (ent.AppMediaRoomUID > 0)
                ret = ret.Where(a => a.AppMediaRoomUID == ent.AppMediaRoomUID);
            if (ent.AppMediaRoomRelatedUID > 0)
                ret = ret.Where(a => a.AppMediaRoomRelatedUID == ent.AppMediaRoomRelatedUID);
            if (ent.RelatedUID > 0)
                ret = ret.Where(a => a.RelatedUID == ent.RelatedUID);
            if (ent.MediaRoomUID > 0)
                ret = ret.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (ent.MediaRoomRelatedUID > 0)
                ret = ret.Where(a => a.MediaRoomRelatedUID == ent.MediaRoomRelatedUID);
            if (!string.IsNullOrEmpty(ent.Category))
                ret = ret.Where(a => a.Category == ent.Category);
            if (IsAddDeletedForSelect)
                ret = ret.Where(a=>a.Deleted==ent.Deleted);
            return ret.ToList();
        }

        public List<M_MediaRoomRelatedDetail> GetList(app_tblMediaRoom_Related ent, bool IsAddDeletedForSelect)
        {
            var ret = from related in model.app_tblMediaRoom_Related
                      select new M_MediaRoomRelatedDetail()
                      {
                          AppMediaRelatedUID = related.AppMediaRoomRelatedUID,
                          AppMediaUID = related.AppMediaRoomUID > 0? (int)related.AppMediaRoomUID: 0,
                          TblMediaUID = related.MediaRoomUID > 0 ? (int)related.MediaRoomUID : 0,
                          AppRelatedUID = related.AppRelatedUID > 0 ? (int)related.AppRelatedUID : 0,
                          TblRelatedUID = related.RelatedUID > 0 ? (int)related.RelatedUID : 0,
                          TblMediaRelatedUID = related.MediaRoomRelatedUID > 0 ? (int)related.MediaRoomRelatedUID : 0,
                          CateGory = related.Category,
                          From = "app",
                          IsDeleted = related.Deleted
                      };
            if (ent.AppMediaRoomUID > 0)
                ret = ret.Where(a => a.AppMediaUID == ent.AppMediaRoomUID);
            if (ent.MediaRoomUID > 0)
                ret = ret.Where(a => a.TblMediaUID == ent.MediaRoomUID);
            if (ent.AppRelatedUID > 0)
                ret = ret.Where(a => a.AppRelatedUID == ent.AppRelatedUID);
            if (ent.RelatedUID > 0)
                ret = ret.Where(a => a.TblRelatedUID == ent.RelatedUID);
            if (ent.MediaRoomRelatedUID > 0)
                ret = ret.Where(a => a.TblMediaRelatedUID == ent.MediaRoomRelatedUID);
            if (ent.AppMediaRoomRelatedUID > 0)
                ret = ret.Where(a => a.AppMediaRelatedUID == ent.AppMediaRoomRelatedUID);
            if (!string.IsNullOrEmpty(ent.Category))
                ret = ret.Where(a => a.CateGory == ent.Category);
            if (IsAddDeletedForSelect)
                ret = ret.Where(a => a.IsDeleted == ent.Deleted);
            return ret.ToList();
        }

        public int GetCountByENT(app_tblMediaRoom_Related ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblMediaRoom_Related ent)
        {
            var ret = model.app_tblMediaRoom_Related.Where(a=>a.AppMediaRoomRelatedUID==ent.AppMediaRoomRelatedUID).FirstOrDefault();
            if (ret != null)
            {
                ret.AppMediaRoomUID = ent.AppMediaRoomUID;
                ret.AppRelatedUID = ent.AppRelatedUID;
                ret.Category = ent.Category;
                ret.Deleted = ent.Deleted;
                ret.IsPublish = ent.IsPublish;
                ret.MediaRoomRelatedUID = ent.MediaRoomRelatedUID;
                ret.MediaRoomUID = ent.MediaRoomUID;
                ret.RelatedUID = ent.RelatedUID;
                ret.Updated_At = DateTime.Now;
                return model.SaveChanges();
            }
            return 0;
        }

        public int DeleteENT(app_tblMediaRoom_Related ent)
        {
            var ret = model.app_tblMediaRoom_Related.Where(a=>a.AppMediaRoomRelatedUID==ent.AppMediaRoomRelatedUID).FirstOrDefault();
            if (ret != null)
            { model.app_tblMediaRoom_Related.DeleteObject(ret); }
            return model.SaveChanges();
        }


        public List<app_tblMediaRoom_Related> GetMaxAppRelatedENTByCUID(app_tblMediaRoom_Related ent)
        {
            var result = from relatedENT in model.tblMediaRoom_Related
                         join AppRelatedENT in model.app_tblMediaRoom_Related
                         on relatedENT.MediaRoomRelatedUID equals AppRelatedENT.MediaRoomRelatedUID
                         where AppRelatedENT.RelatedUID == ent.RelatedUID && AppRelatedENT.Category==ent.Category && relatedENT.Deleted == 0 && AppRelatedENT.Deleted == 0
                         orderby AppRelatedENT.AppMediaRoomRelatedUID descending
                         select AppRelatedENT;
            return result.ToList();
        }

        public List<Ext_MediaRoom> GetListWithMediaRoomByENT(Ext_MediaRoom ent, bool value)
        {
            var result = from mrr in model.app_tblMediaRoom_Related
                         join mr in model.tblMediaRooms on mrr.MediaRoomUID equals mr.MediaRoomUID
                         where mrr.MediaRoomUID > 0 && mr.MediaRoomUID > 0 && mrr.RelatedUID==ent.RelatedUID
                         select new Ext_MediaRoom
                         {
                             MediaRoomRelatedUID =mrr.AppMediaRoomRelatedUID,
                             Category = mrr.Category,
                             RelatedUID = (int)mrr.RelatedUID,
                             Deleted = mrr.Deleted,
                             MediaRoomUID = mr.MediaRoomUID,
                             MediaType = mr.MediaType,
                             FilePath = mr.FilePath,
                             FileName = mr.FileName,
                             FileAlt_Eng = mr.FileAlt_Eng,
                             FileAlt_Chi = mr.FileAlt_Eng,
                             FileLink_Eng = mr.FileLink_Eng,
                             FileLink_Chi = mr.FileLink_Chi,
                             Des_Eng = mr.Des_Eng,
                             Des_Chi = mr.Des_Chi,
                             IsPublish = mrr.IsPublish,//关联的字段
                             LastModifyBy = mr.LastModifyBy,
                             CreatedAt = mr.CreatedAt,
                             UpdatedAt = mr.UpdatedAt
                         };
            
            if (ent.MediaRoomUID > 0)
                result = result.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.RelatedUID > 0)
                result = result.Where(a => a.RelatedUID == ent.RelatedUID);
            result = result.Where(a => a.Deleted == ent.Deleted);
            return result.OrderByDescending(a => a.CreatedAt).ToList<Ext_MediaRoom>();
        }

        public List<Ext_MediaRoom> GetListWithMediaRoomByENT(Ext_MediaRoom ent)
        {
            var result = from mrr in model.app_tblMediaRoom_Related
                         join mr in model.app_tblMediaRoom on mrr.AppMediaRoomUID equals mr.AppMediaRoomUID
                         where mrr.AppMediaRoomUID > 0 && mr.AppMediaRoomUID > 0
                         select new Ext_MediaRoom
                         {
                             MediaRoomRelatedUID = mrr.AppMediaRoomRelatedUID,
                             Category = mrr.Category,
                             RelatedUID = (int)mrr.RelatedUID,
                             Deleted = mrr.Deleted,
                             MediaRoomUID = mr.AppMediaRoomUID,
                             MediaType = mr.MediaType,
                             FilePath = mr.FilePath,
                             FileName = mr.FileName,
                             FileAlt_Eng = mr.FileAlt_Eng,
                             FileAlt_Chi = mr.FileAlt_Eng,
                             FileLink_Eng = mr.FileLink_Eng,
                             FileLink_Chi = mr.FileLink_Chi,
                             Des_Eng = mr.Des_Eng,
                             Des_Chi = mr.Des_Chi,
                             IsPublish = mrr.IsPublish,//关联表的字段
                             Is_Publish = mr.IsPublish,
                             LastModifyBy = mr.LastModifyBy,
                             CreatedAt = mr.CreatedAt,
                             UpdatedAt = mr.UpdatedAt
                         };
            if (ent.MediaRoomUID > 0)
                result = result.Where(a => a.MediaRoomUID == ent.MediaRoomUID);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (!string.IsNullOrEmpty(ent.MediaType))
                result = result.Where(a => a.MediaType == ent.MediaType);
            if (ent.RelatedUID > 0)
                result = result.Where(a => a.RelatedUID == ent.RelatedUID);
            if (ent.Is_Publish >= 0)
                result = result.Where(a => a.Is_Publish == ent.Is_Publish);
            result = result.Where(a => a.Deleted == ent.Deleted);
            return result.OrderByDescending(a => a.CreatedAt).ToList<Ext_MediaRoom>();
        }

    }
}
